package com.tuuzed.hydrogen.simple;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tuuzed.hydrogen.simple.model.CapTestModel;
import com.tuuzed.hydrogen.simple.quickstart.QuickStartActivity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button mBtnQuickStart;
    private Button mBtnCapTest;
    private TextView mTextTestResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mBtnQuickStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, QuickStartActivity.class));
            }
        });
        mBtnCapTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                capTest();
            }
        });

    }

    private void capTest() {
        mBtnCapTest.setEnabled(false);
        mBtnCapTest.setText("正在测试");
        new AsyncTask<Void, Void, String>() {
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                mTextTestResult.setText("测试结果: \n" + result);
                mBtnCapTest.setEnabled(true);
                mBtnCapTest.setText(R.string.cap_test);
            }

            @Override
            protected String doInBackground(Void... params) {
                StringBuilder result = new StringBuilder();


                List<CapTestModel> capTestModels = new ArrayList<>();
                for (int i = 0; i < 10_000; i++) {
                    CapTestModel capTestModel = new CapTestModel();
                    capTestModel.setCreateDate(new Date());
                    capTestModel.setPassword("password" + Math.random());
                    capTestModel.setAge(i);
                    capTestModel.setName("name" + Math.random());
                    capTestModel.setSex("sex" + Math.random());
                    capTestModels.add(capTestModel);
                }
                CapTestModel.delete(CapTestModel.class, null, null);
                // 循环插入10_000条
                long start = System.currentTimeMillis();
                for (CapTestModel user : capTestModels) {
                    CapTestModel.insert(user);
                }
                String tmp = "循环插入10_000条: " + (System.currentTimeMillis() - start) + " ms";
                Log.d(TAG, tmp);
                result.append(tmp).append("\n");
                CapTestModel.delete(CapTestModel.class, null, null);
                // 批量插入10_000条
                start = System.currentTimeMillis();
                CapTestModel.insertTx(capTestModels);
                tmp = "批量插入10_000条: " + (System.currentTimeMillis() - start) + " ms";
                Log.d(TAG, tmp);
                result.append(tmp).append("\n");



                // 循环更新10_000条
                start = System.currentTimeMillis();
                for (CapTestModel capTestModel : capTestModels) {
                    capTestModel.setAge(capTestModel.getAge() + 1);
                    CapTestModel.update(capTestModel);
                }
                tmp = "循环更新10_000条: " + (System.currentTimeMillis() - start) + " ms";
                Log.d(TAG, tmp);
                result.append(tmp).append("\n");
                // 批量更新10_000条
                for (CapTestModel capTestModel : capTestModels) {
                    capTestModel.setAge(capTestModel.getAge() + 1);
                }
                start = System.currentTimeMillis();
                CapTestModel.updateTx(capTestModels);
                tmp = "批量更新10_000条: " + (System.currentTimeMillis() - start) + " ms";
                Log.d(TAG, tmp);
                result.append(tmp).append("\n");



                // 批量查询10_000条
                start = System.currentTimeMillis();
                List<CapTestModel> capTestModelList = CapTestModel.findAll(CapTestModel.class);
                tmp = "批量查询10_000条: " + (System.currentTimeMillis() - start) + " ms";
                Log.d(TAG, tmp);
                result.append(tmp).append("\n");
                // 循环查询10_000条
                start = System.currentTimeMillis();
                for (CapTestModel capTestModel : capTestModelList) {
                    CapTestModel cap = CapTestModel.findById(CapTestModel.class, capTestModel.getId());
                }
                tmp = "循环查询10_000条: " + (System.currentTimeMillis() - start) + " ms";
                Log.d(TAG, tmp);
                result.append(tmp).append("\n");
                return result.toString();
            }
        }.execute();
    }

    private void initView() {
        mBtnQuickStart = (Button) findViewById(R.id.btnQuickStart);
        mBtnCapTest = (Button) findViewById(R.id.btnCapTest);
        mTextTestResult = (TextView) findViewById(R.id.textTestResult);
    }
}
